{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6dd61309",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd,xlwings as xw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "35b5893a",
   "metadata": {},
   "outputs": [],
   "source": [
    "wbook1= xw.Book(r\"C:\\Users\\UUPT\\Desktop\\商城订单1.25.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b3fa2d1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1=wbook1.sheets(1).range(\"a1\").expand().options(pd.DataFrame).value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "54eae2ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2=df1.groupby(by=[\"城市名称\",\"商品编号\",\"商品名称\"])[[\"数量\",\"在线支付时价格\",\"混合支付时的在线支付金额\"]].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "c1be9611",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2[\"在线支付金额合计\"]=df2[\"在线支付时价格\"]+df2[\"混合支付时的在线支付金额\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "f01b34b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "套装df=pd.read_excel(r\"C:\\Users\\UUPT\\Desktop\\商品管理-套装1.25.xlsx\").loc[:,['商品编号','商品名称','商品原价(元)','套装中单品编号','套装中单品名称','套装中单品数量','套装中单品原价(元)']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "5f8866f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "lista=df3['商品名称'].drop_duplicates().tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "dd2d1948",
   "metadata": {},
   "outputs": [],
   "source": [
    "套装df=套装df.query(\"商品名称 in @lista\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "6cccb362",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_new_table(group_data):\n",
    "    # 在这里根据组数据进行逻辑处理，生成新的表格\n",
    "    # 这里只是简单地将每个组的数据乘以2作为示例\n",
    "    group_data['套装中单品价格（数量×单价）'] = group_data['套装中单品原价(元)']*group_data['套装中单品数量']\n",
    "    group_data['套装原价(单品和)'] = group_data['套装中单品价格（数量×单价）'].sum()\n",
    "    group_data['单件金额占比']=group_data['套装中单品价格（数量×单价）']/group_data['套装原价(单品和)']\n",
    "    group_date['套装中单品价格(折扣后)']=group_data['商品原价'] *套装df['单件金额占比']\n",
    "    return group_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "be327e68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "138"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(套装df.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "f56f9e58",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "91da26ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "套装df.to_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "c0914034",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>城市名称</th>\n",
       "      <th>商品编号</th>\n",
       "      <th>商品名称</th>\n",
       "      <th>数量</th>\n",
       "      <th>在线支付时价格</th>\n",
       "      <th>混合支付时的在线支付金额</th>\n",
       "      <th>在线支付金额合计</th>\n",
       "      <th>商品原价(元)</th>\n",
       "      <th>套装中单品编号</th>\n",
       "      <th>套装中单品名称</th>\n",
       "      <th>套装中单品数量</th>\n",
       "      <th>套装中单品原价(元)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>东莞市</td>\n",
       "      <td>1584.0</td>\n",
       "      <td>基础套装（2）</td>\n",
       "      <td>53.0</td>\n",
       "      <td>2644.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2644.7</td>\n",
       "      <td>49.9</td>\n",
       "      <td>71.0</td>\n",
       "      <td>夏季马甲</td>\n",
       "      <td>1.0</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>东莞市</td>\n",
       "      <td>2874.0</td>\n",
       "      <td>冲锋衣亲民版活动</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>1597.0</td>\n",
       "      <td>冲锋衣（亲民版）</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>东营市</td>\n",
       "      <td>1590.0</td>\n",
       "      <td>汽车套装</td>\n",
       "      <td>2.0</td>\n",
       "      <td>530.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>530.0</td>\n",
       "      <td>265.0</td>\n",
       "      <td>1585.0</td>\n",
       "      <td>培训报名</td>\n",
       "      <td>1.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>东营市</td>\n",
       "      <td>1590.0</td>\n",
       "      <td>汽车套装</td>\n",
       "      <td>2.0</td>\n",
       "      <td>530.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>530.0</td>\n",
       "      <td>265.0</td>\n",
       "      <td>1586.0</td>\n",
       "      <td>冲锋衣</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>东营市</td>\n",
       "      <td>1590.0</td>\n",
       "      <td>汽车套装</td>\n",
       "      <td>2.0</td>\n",
       "      <td>530.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>530.0</td>\n",
       "      <td>265.0</td>\n",
       "      <td>1587.0</td>\n",
       "      <td>马甲</td>\n",
       "      <td>1.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>248</th>\n",
       "      <td>长沙市</td>\n",
       "      <td>2910.0</td>\n",
       "      <td>长沙特价套装</td>\n",
       "      <td>3.0</td>\n",
       "      <td>477.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>477.0</td>\n",
       "      <td>159.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>夏季马甲</td>\n",
       "      <td>1.0</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>249</th>\n",
       "      <td>长沙市</td>\n",
       "      <td>2910.0</td>\n",
       "      <td>长沙特价套装</td>\n",
       "      <td>3.0</td>\n",
       "      <td>477.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>477.0</td>\n",
       "      <td>159.0</td>\n",
       "      <td>2603.0</td>\n",
       "      <td>保温箱（普通版）</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>250</th>\n",
       "      <td>长治市</td>\n",
       "      <td>934.0</td>\n",
       "      <td>秋季套装含头盔（新）</td>\n",
       "      <td>1.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>保温箱</td>\n",
       "      <td>1.0</td>\n",
       "      <td>120.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251</th>\n",
       "      <td>长治市</td>\n",
       "      <td>934.0</td>\n",
       "      <td>秋季套装含头盔（新）</td>\n",
       "      <td>1.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>头盔</td>\n",
       "      <td>1.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252</th>\n",
       "      <td>长治市</td>\n",
       "      <td>934.0</td>\n",
       "      <td>秋季套装含头盔（新）</td>\n",
       "      <td>1.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>269.0</td>\n",
       "      <td>903.0</td>\n",
       "      <td>冲锋衣（新版）</td>\n",
       "      <td>1.0</td>\n",
       "      <td>139.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>253 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    城市名称    商品编号        商品名称    数量  在线支付时价格  混合支付时的在线支付金额  在线支付金额合计  商品原价(元)  \\\n",
       "0    东莞市  1584.0     基础套装（2）  53.0   2644.7           0.0    2644.7     49.9   \n",
       "1    东莞市  2874.0    冲锋衣亲民版活动   3.0      0.0         300.0     300.0     99.0   \n",
       "2    东营市  1590.0        汽车套装   2.0    530.0           0.0     530.0    265.0   \n",
       "3    东营市  1590.0        汽车套装   2.0    530.0           0.0     530.0    265.0   \n",
       "4    东营市  1590.0        汽车套装   2.0    530.0           0.0     530.0    265.0   \n",
       "..   ...     ...         ...   ...      ...           ...       ...      ...   \n",
       "248  长沙市  2910.0      长沙特价套装   3.0    477.0           0.0     477.0    159.0   \n",
       "249  长沙市  2910.0      长沙特价套装   3.0    477.0           0.0     477.0    159.0   \n",
       "250  长治市   934.0  秋季套装含头盔（新）   1.0    269.0           0.0     269.0    269.0   \n",
       "251  长治市   934.0  秋季套装含头盔（新）   1.0    269.0           0.0     269.0    269.0   \n",
       "252  长治市   934.0  秋季套装含头盔（新）   1.0    269.0           0.0     269.0    269.0   \n",
       "\n",
       "     套装中单品编号   套装中单品名称  套装中单品数量  套装中单品原价(元)  \n",
       "0       71.0      夏季马甲      1.0        50.0  \n",
       "1     1597.0  冲锋衣（亲民版）      1.0        99.0  \n",
       "2     1585.0      培训报名      1.0       150.0  \n",
       "3     1586.0       冲锋衣      1.0       100.0  \n",
       "4     1587.0        马甲      1.0        30.0  \n",
       "..       ...       ...      ...         ...  \n",
       "248     71.0      夏季马甲      1.0        50.0  \n",
       "249   2603.0  保温箱（普通版）      1.0        99.0  \n",
       "250     20.0       保温箱      1.0       120.0  \n",
       "251     22.0        头盔      1.0        60.0  \n",
       "252    903.0   冲锋衣（新版）      1.0       139.0  \n",
       "\n",
       "[253 rows x 12 columns]"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = pd.merge(df2.reset_index(),套装df,on=[\"商品编号\",\"商品名称\"],how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "779310fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "result=pd.DataFrame()\n",
    "for i,j in 套装df.groupby(by=['商品编号','商品名称']):\n",
    "    j.reset_index(drop=True,inplace=True)\n",
    "    j['单品价格（数量×单价）']=j['套装中单品数量']*j['套装中单品原价(元)']\n",
    "    j['套装价（单品价格和）']=j['单品价格（数量×单价）'].sum()\n",
    "    j['单品金额占比']=round(j['单品价格（数量×单价）'] / j['套装价（单品价格和）'],4)\n",
    "    row=len(j.index)-1\n",
    "    j.loc[row,'单品金额占比']=round(1-(j['单品金额占比'].sum()-j.loc[row,'单品金额占比']),4)\n",
    "    result=pd.concat([result,j])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "a1eefd79",
   "metadata": {},
   "outputs": [],
   "source": [
    "result.to_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7727b33a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
